package spotengine;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Calendar;

/**
 *
 */
public class Message {

    public Message(String sender, String receiver, String body) {
        this.sender = sender;
        this.receiver = receiver;
        this.body = body;
        this.timestamp = Calendar.getInstance().getTime().getTime();
    }

    private Message() {
    }
    private String sender;
    private String receiver;
    private String body;
    private long timestamp;

    public String getSender() {
        return sender;
    }

    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (o == this) {
            return true;
        }
        if (!(o instanceof Message)) {
            return false;
        } else {
            Message other = (Message) o;
            return this.timestamp == other.timestamp;
        }

    }

    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + (int) (this.timestamp ^ (this.timestamp >>> 32));
        return hash;
    }

    public static Message decode(DataInput in) throws IOException {
        Message msg = new Message();
        msg.sender = in.readUTF();
        msg.receiver = in.readUTF();
        msg.body = in.readUTF();
        msg.timestamp = in.readLong();
        return msg;
    }

    public void writeTo(DataOutput out) throws IOException {
        out.writeUTF(sender);
        out.writeUTF(receiver);
        out.writeUTF(body);
        out.writeLong(timestamp);
    }

    String getBody() {
        return body;
    }
}
